package handler

import (
	"svvenus/conf"
	"svvenus/util/uttype"
	"svvenus/vgame/logic"
	"svvenus/znet"
	"svvenus/znet/zpb"
)

// CGPlayerChangeName
// @Description: 玩家改名
// @param request
func CGPlayerChangeName(request *znet.Request) {
	rid := zpb.EPid_GCPlayerChangeName
	var req zpb.MsgString
	if !request.GetPb(&req) {
		return
	}

	player := logic.ConnGetPlayer(request.GetConnection())
	if player == nil {
		request.SendCode(rid, conf.EProtocolResult玩家未登录)
		return
	}

	checkName := logic.PlayerCheckName(req.Value)
	if checkName != 0 {
		request.SendCode(rid, checkName)
		return
	}

	player.Name = req.Value

	request.SendCode(rid, 0)
}

// CGPlayerChangeHead
// @Description: 更改头像
// @param request
func CGPlayerChangeHead(request *znet.Request) {
	rid := zpb.EPid_GCPlayerChangeHead
	var req zpb.MsgInt32
	if !request.GetPb(&req) {
		return
	}

	player := logic.ConnGetPlayer(request.GetConnection())
	if player == nil {
		request.SendCode(rid, conf.EProtocolResult玩家未登录)
		return
	}

	player.Head = int(req.Value)

	request.SendCode(rid, 0)
}

// CGPlayerChangeFlagInt
// @Description: 客户端标记
// @param request
func CGPlayerChangeFlagInt(request *znet.Request) {
	var req zpb.MsgKvInt32Int32
	if !request.GetPb(&req) {
		return
	}

	player := logic.ConnGetPlayer(request.GetConnection())
	if player == nil {
		return
	}
	player.FlagInt[uttype.Int32ToString(req.Key)] = req.Value
}

// CGPlayerChangeFlagIntDaily
// @Description: 客户端标记日更新
// @param request
func CGPlayerChangeFlagIntDaily(request *znet.Request) {
	var req zpb.MsgKvInt32Int32
	if !request.GetPb(&req) {
		return
	}

	player := logic.ConnGetPlayer(request.GetConnection())
	if player == nil {
		return
	}
	player.FlagDailyInt[uttype.Int32ToString(req.Key)] = req.Value
}

// CGPlayerInit
// @Description: 玩家信息获取
// @param request
func CGPlayerInit(request *znet.Request) {
	rsp := &zpb.PlayerInit{}
	rid := zpb.EPid_GCPlayerInit

	player := logic.ConnGetPlayer(request.GetConnection())
	if player == nil {
		request.Send(rid, conf.EProtocolResult玩家未登录, rsp)
		return
	}
	rsp = player.InitInfo()

	request.Send(rid, 0, rsp)
}
